package com.dididoctor.doctor.Activity.Usercentre.Authentication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.MV.adapter.CommonAdapter;
import com.dididoctor.doctor.MV.adapter.ViewHolder;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.cityList.CityBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by boy on 2016/4/22.
 */
public class SelectAddressActivity extends EduActivity {
    private LinearLayout mLlSelectCity;
    private EditText mEtKeyword;
    private Button mBtnCancelKeyword;
    private final static int EDITOR_CITY = 108;
    private String cityId = "", cityName = "";
    private TextView mTvCityname;
    private AddressAdapter adapter;
    private PoiSearch mPoiSearch = null;
    private ListView lv_keyword_city;
    private List<PoiInfo> poiInfos = new ArrayList<>();
    private Button btn_cancel_keyword;
    private  List<PoiInfo> allPoi = new ArrayList<PoiInfo>();
    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_select_address);
        cityId = getIntent().getStringExtra("cityId");
        cityName = getIntent().getStringExtra("cityName");
    }

    @Override
    public void init() {
        mLlSelectCity = (LinearLayout) findViewById(R.id.ll_select_city);
        mLlSelectCity.setOnClickListener(this);
        mEtKeyword = (EditText) findViewById(R.id.et_keyword);
        mEtKeyword.setOnClickListener(this);
        mBtnCancelKeyword = (Button) findViewById(R.id.btn_cancel_keyword);
        mBtnCancelKeyword.setOnClickListener(this);
        mTvCityname = (TextView) findViewById(R.id.tv_cityname);
        mTvCityname.setText(cityName);
        lv_keyword_city = (ListView) findViewById(R.id.lv_keyword_city);

        adapter = new AddressAdapter(SelectAddressActivity.this, allPoi, 0);
        lv_keyword_city.setAdapter(adapter);

        mEtKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub
                //关键是这里,监听输入的字符串,如果大于零,则可点击,enable.
                if (s.length() > 1) {
                    mPoiSearch.searchInCity((new PoiCitySearchOption())
                            .city(cityName)
                            .keyword(mEtKeyword.getText().toString().trim())
                            .pageNum(10));
                }
            }
        });

        //百度地图
        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(poiListener);


    }

    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
        public void onGetPoiResult(PoiResult result) {
            //获取POI检索结果

            if (result == null
                    || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                Toast.makeText(SelectAddressActivity.this, "未找到结果", Toast.LENGTH_LONG)
                        .show();
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {

            } else {
                Toast.makeText(SelectAddressActivity.this, "未找到结果", Toast.LENGTH_LONG)
                        .show();
                return;

            }
            allPoi= result.getAllPoi();
            adapter = new AddressAdapter(SelectAddressActivity.this, allPoi, 0);
            lv_keyword_city.setAdapter(adapter);

        }

        public void onGetPoiDetailResult(PoiDetailResult result) {
            //获取Place详情页检索结果
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPoiSearch.destroy();
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.ll_select_city:
                Intent intent = new Intent(this, EditCityActivity.class);
                startActivityForResult(intent, EDITOR_CITY);
                break;
            case R.id.btn_cancel_keyword:
                mEtKeyword.setText("");
                adapter.getData().clear();
                adapter.notifyDataSetChanged();
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case EDITOR_CITY:
                    CityBean cityBean = (CityBean) data.getSerializableExtra("hotcity");
                    cityName = cityBean.getCityName();
                    if (cityName != null && !cityName.equals("")) {
                        mTvCityname.setText(cityName);
                    }

                    String cityCode = cityBean.getCityCode();
                    if (cityCode != null && cityCode.equals("")) {
                        cityId = cityCode;
                    }
                    break;
                default:
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public class AddressAdapter extends CommonAdapter<PoiInfo> {
        private Context mcontext;


        public AddressAdapter(Context context, List<PoiInfo> datas, int itemLayoutId) {
            super(context, datas, R.layout.item_addresslist);
            this.mcontext = context;
        }

        @Override
        public void convert(int position, ViewHolder helper, final PoiInfo item) {
            TextView view = helper.getView(R.id.addressname);
            view.setText(item.address);

            helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, BaiDuMapActivity.class);
                    intent.putExtra("addressStr", item.address);
                    intent.putExtra("latitude", item.location.latitude);
                    intent.putExtra("longitude", item.location.longitude);
                    intent.putExtra("cityname", item.city);
                    intent.putExtra("cityId", cityId);
                    ((Activity) context).setResult(Activity.RESULT_OK, intent);
                    ((Activity) context).finish();
                }
            });
        }
    }


}
